<?php
/**
 * Created by PhpStorm.
 * User: 郑合达
 * Date: 2019/1/22
 * Time: 16:20
 */

namespace app\admin\controller;


use app\model\CardModel;
use app\model\CardTypeModel;
use app\model\DeviceModel;
use app\model\EnclosureLogModel;
use app\model\EnterLogModel;
use app\model\EquipmentModel;
use app\model\UserModel;
use think\Controller;

class Recharge extends Common
{

    /**
     * @return \think\response\View
     * 点卡生成
     */
    public function generate()
    {
        if (session('user.type')!=1){$this->error('非法操作');}
        //筛选
        $postData=input('get.');
        $where=[];
        if (isset($postData['type'])){
            if ($postData['type']!=-1){//类型
                $where['type'] = ['=', $postData['type']];
            }
            if ($postData['display']!=-1){//是否使用
                $where['display'] = ['=', $postData['display']];
            }
        }
        //点卡列表
        $data['card']=$card= CardModel::where($where)->paginate(null,false,['query'=>request()->param()]);
        return view('generate',$data);
    }
/*
 * 充值套餐类型--页面
 */
    public function generate2()
    {
        $data['data']=CardTypeModel::paginate(null,false,['query'=>request()->param()]);
        return view('generate2_view',$data);
     }
    /*
    * 充值套餐类型--编辑页面
    */
    public function generate2_add()
    {
        $getId=input('id');
        $data['data']=CardTypeModel::find($getId);
        return view('generate2_add',$data);

    }
    /*
     * 充值套餐类型--编辑
     */
    public function gen2_add()
    {
        $getData=input('post.');
        $updata=[
            'id'=>$getData['cy_id'],
            'name'=>$getData['cy_name'],
            'price'=>$getData['cy_price'],
            'overtime'=>$getData['overtime'],
            ];
        $setCard=CardTypeModel::update($updata);
      return  $this->setDatabase($setCard);
    }
    /**
     * @return \think\response\Json
     * 添加点卡
     */
    public function add_generate()
    {
        if (session('user.type')!=1){$this->error('非法操作');}
        $getData=input('post.');
//        dump($getData);die();
        $data=[];
        $strs="QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
        for ($i=0;$i<$getData['num'];$i++){
            $name=time().rand(0, 99999);
            $pass=substr(str_shuffle($strs),mt_rand(0,strlen($strs)-7),10).rand(0,99999999);;
            $data[$i]=['name'=>$name,
                        'pass'=>$pass,
                        'type'=>$getData['state']];
        }
        $card = new CardModel();
       $setCard=$card->saveAll($data);
        if($setCard){
            return retJson('',200,'添加完成');
        }
        return retJson('',201,'添加失败');
//        dump($data);die();
    }

    /**
     * @return \think\response\View
     * 点数充值页面
     */
    public function recharge(){

        return view('recharge');
    }
/*
 * 充值
 */
    public function add_recharge()
    {
        $getData=input('post.');
        $where=['name'=>$getData['name'],
                'pass'=>$getData['pass']];
        $card=CardModel::where($where)->find();//查找点卡
        if (!$card){return retJson('',201,'请检查点卡账号密码');}
        if ($card->display){return retJson('',201,'点卡已使用');}

        $device=EquipmentModel::where('name',$getData['Devid'])->find();//查找设备
        if (!$device){return retJson('',201,'设备号不存在');}
        $data= $this->recha_time_type($card->type);
        $expire=$device->expire_time;
        if ($expire>time()){//设备还没到期
            $expire+=$data['0'];
        }else{//设备已到期
            $expire=time()+$data['0'];
        }
        //更改点卡状态
        $updata=['id'=>$card->id,'display'=>1];
        $setCard=CardModel::update($updata);
        setDatabaseDef($setCard,'点卡状态更改失败');
        $updata2=['expire_time'=>$expire];
        $setDevice=EquipmentModel::where('id',$device->id)->update($updata2);//该设备增加到期时间
        //添加充值记录
        $inser_En=['num'=>$data['2'],'imei'=>$device['name'],'card_name'=>$getData['name'],'addtime'=>time()];
        EnterLogModel::insert($inser_En);
       return setDatabase($setDevice,'充值');

    }
    /**
     * @return \think\response\View
     * 充值卡转让
     */
    public function recharge_card(){
        return view('recharge_card');
    }
    /*
     * 导出点卡列表
     */
    public function export(){
        $getData = input('get.');
//      dump($user_id);die();
        $where=[];
         if ($getData['type']!=-1){
             $where['type']=$getData['type'];
         }
         if($getData['display']!=-1){
             $where['display']=$getData['display'];
         }
        $data =CardModel::field('*')->where($where)->order('id')->select();
//         dump($data);die();
        //表头
        $header = ['序号','类型','点卡号','密码','是否使用'];
        //用于遍历的二维数组
        $result_data=array();
        $type=[0=>'月卡',1=>'季卡',2=>'年卡'];
        $display=[0=>'未使用',1=>'已使用'];
        for($i=0;$i<count($data);$i++){
            $new = array();
            $new[] = $i+1;
            $new[] = $type[$data[$i]['type']];
            $new[] =  $data[$i]['name'];
            $new[] =  $data[$i]['pass'];

            $new[] = $display[$data[$i]['display']];
            array_push($result_data,$new);
        }
        //$filename 文件名
        $filename = "点卡列表".date("Y/m/d");
//        ExcelModel::getExcelFile(NULL,$header,$result_data,$filename,30);
        export($result_data, $header, $filename);
    }
    /**
     * @return \think\response\View
     * 充值记录
     */
    public function consumption(){
        $postData=input('get.');
        $where=[];
        $whe=$this->equs();
        if ($whe){
            $where=['imei'=>['in',$whe]];
        }
        if (isset($postData['type'])){
            if ($postData['type']!=-1){//类型
                $where['num'] = ['=', $postData['type']];
            }
        }
       $data['data']= EnterLogModel::where($where)->paginate(null,false,['query'=>request()->param()]);
        return view('consumption',$data);
    }

    /*
     * 点数转让--检测登陆账号
     */
    public function detection()
    {
        $getData=input('post.');
        $where=['u.user_name'=>$getData['user_name']];
//        $where=['u.user_name'=>'bdty'];
        $getdatabase=UserModel::alias('u')
            ->join('gps_menu m','u.id= m.user_id')
            ->where($where)
            ->find();
        if($getdatabase){
            return retJson($getdatabase->name,200,'');
        }
        return retJson('',201,'无匹配');

    }
    /*
    * 点数转让
    */
    public function presenter()
    {
        $getData=input('post.');
//        $where=['u.user_name'=>$getData['user_name']];
////        $where=['u.user_name'=>'bdty'];
//        $getdatabase=UserModel::alias('u')
//            ->join('gps_menu m','u.id= m.user_id')
//            ->where($where)
//            ->find();
//        if($getdatabase){
//            return retJson($getdatabase->name,200,'');
//        }
        return retJson('',201,'点数不足'.$getData['user_name']);
    }

    /**
     * 点卡单删
     */
    public function del(){
        $postData = input('post.');
        $del = CardModel::destroy($postData['id']);
        return $this->setDatabase($del);
    }
    /**
     * 点卡批量删删
     */
    public function fulldel(){
        $postData = input('post.');
        $del = CardModel::destroy($postData['id']);
        return $this->setDatabase($del);
    }
}